<?php
namespace ColorIdentify\Controller;
use Common\Controller\ColorIdentifyController;
class IndexController extends ColorIdentifyController {

    public function playerscore()
    {
        $data['level']      =   I('post.level','','int');
        $data['nick']       =   I('post.nick','','htmlspecialchars');
        $data['pic']        =   I('post.pic','','htmlspecialchars');
        $third              =   I('post.third','','htmlspecialchars');
        $data['time']       =   I('post.useTime','','htmlspecialchars');
        $data['update_time']=   time();
        $share_uid          =   I('post.share_uid','','htmlspecialchars');
        $form_id            =   I('post.form_id','','htmlspecialchars');

        if (empty($third) )
        {
            $this->returnData('','third is  not exist',1001);
        }

        $where['third'] =   $third;
        $field      =   array('colour_member_id','third','old_level','level','record');
        $user_data  =   M('colour_member')->where($where)->field($field)->find();
        if (empty($user_data))
        {
            $this->returnData('','third 99 is not exist',1001);
        }

        //绑定好友
        if (!empty($share_uid))
        {
            $this->make_friend($user_data['colour_member_id'] ,$share_uid);
        }

        //处理表单form-id
        if (! empty($form_id))
        {
            $this->send($user_data, $form_id);
        }

        $now_level      =   $data['level'];
        $old_level      =   0;

        if ( ! empty($user_data['old_level']))
        {
            $data['old_level']  =   $user_data['level'];    //更新最近一次玩的等级
            $old_level          =   $user_data['level'];
        }
        elseif (empty($user_data['old_level']) && ! empty($user_data['level']))
        {
            $data['old_level']  =   $user_data['level'];    //更新最近一次玩的等级
        }

        if (empty($data['nick']))
        {
            unset($data['nick']);
        }

        if (empty($data['pic']))
        {
            unset($data['pic']);
        }

        $data['record']     =   $user_data['record'] + 1;   //挑战次数

        $result     =   M('colour_member')->where($where)->data($data)->save(); // 根据条件保存修改的数据

        if (! empty($result))
        {
            //更新配置的人数
            $this->update_playerScore($now_level,$old_level);
            $total_record   =   M('colour_config')->sum('sum');

            $field           =   array('id','tag','min_level','max_level','sum');
            $config_data     =   M('colour_config')->field($field)->select();

            $data            =  array();
            $data['data']    =  array();
            $data['des']     =  '渣渣灰';
            $data['level']   =  $now_level;
            $data['per']     =  "0%";
            $under_me        =  0;

            if (! empty($config_data))
            {
                foreach ($config_data as $val)
                {
                    $record_data            =   array();
                    $record_data            =   $val;
                    $record_data['index']   =   0;
                    if ($now_level >= $val['min_level'] && $now_level <= $val['max_level'])
                    {
                        $record_data['index']   =   1;
                        $data['des']            =   $val['tag'];
                    }
                    $record_data['progress']    =   number_format($val['sum'] / $total_record * 100 ,2)."%";

                    if ($val['max_level'] < $now_level )
                    {
                        $under_me   =   $under_me + $val['sum'];
                    }

                    $data['data'][]             =   $record_data;
                }
            }

            $ratio          =   $under_me / $total_record * 100;
            $data['per']    =    number_format($ratio ,2) .'%';

            $this->returnData($data,'third is not exist',200);
        }
        else
        {
            $this->returnData('','thirds is not exist',1001);
        }
    }

    /**
     * 存放模板消息
     */
    public function send($member_data, $form_id, $form_type = 1)
    {
        $template_id    =   1;
        $date           =   date('Y-m-d');
        $insert = array(
            'appid'=>'wx66da7c3d2bc96fc0',
            'touser'=>$member_data['third'],
            'template_id'=>$template_id,
            'page'=>'pages/index/index',
            'form_id'=>$form_id,
            'form_type'=>$form_type,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>'e1472ac4b86362d3ce31ff5e66899ed9',
            'createtime'=>$date,
            'colour_member_id'=>$member_data['colour_member_id']
        );

        M('colour_template_send')->data($insert)->add();
        $where['touser']     = $member_data['third'];
        $where['createtime'] = $date;
        $where['status']     = '-1';
        $ishave = M('colour_template_send')->field('wxopen_template_send_id')->where($where)->find();
        if ($ishave)
        {
            M('colour_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }
        else
        {
            M('colour_template_send')->data($insert)->add();
        }
    }

}